简体   繁体   中英

Saving a record from php and mysql

I'm trying to save some values from my query:

$query = mysqli_query($conex, "SELECT * FROM acceso WHERE usuario = '$user' AND contrasenia = '$pass'");
if (mysqli_num_rows($query) > 0)// mysqli_num_rows > 0
{       
    $fila = mysql_fetch_assoc($query)
    $_SESSION["id_usuario_cms"] = $fila ['idacces'];
    $_SESSION["usuario_cms"] = $fila ['user'];
    echo '<script>location.href = "admin/index.php"</script>';
}

but it doesn't work. Any ideas?

I see one potential problem and one obvious problem.

The potential is if you've started the session or not.

session_start(); is required to be used inside all files using sessions; this isn't mentioned in your question. If it's not part of your file(s), add it.

The obvious being that, you're using a mysql_ function being mixed in with the rest of your shown code, being mysqli_ . Those different functions do not intermix with each other.

mysql_fetch_assoc needs to be its mysqli_ equivalent, mysqli_fetch_assoc .

You should be using mysqli_fetch_array for this though.

In order to use mysqli_fetch_assoc , it should be set in a while loop.

Example from the manual:

/* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

If you wish to use mysqli_fetch_array :

Example from the manual:

/* associative array */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Including or die(mysqli_error($conex)) to mysqli_query() .

Sidenote: Error reporting should only be done in staging, and never production.

You have to use mysqli_fetch_array($query) also session_start()

$query = mysqli_query($conex, "SELECT * FROM acceso WHERE usuario = '$user' AND contrasenia = '$pass'");
    if (mysqli_num_rows($query) > 0)// mysqli_num_rows > 0
    {       
        $fila = mysqli_fetch_array($query)
        $_SESSION["id_usuario_cms"] = $fila ['idacces'];
        $_SESSION["usuario_cms"] = $fila ['user'];
        echo '<script>location.href = "admin/index.php"</script>';
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM