SQL: mysqli doesn't work, but neither does mysql_ in part

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sumosalesman
Forum Newbie
Posts: 1
Joined: Mon Apr 13, 2009 6:58 pm

SQL: mysqli doesn't work, but neither does mysql_ in part

Post by sumosalesman »

Hi everyone,

I've run this code through for a few hours, then I ran it through RapidPHP with no syntax errors, and I narrowed it down first to an error in using "mysqli" instead of "mysql". I replaced mysqli with mysql and it still caused errors...

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/findvj/public_html/jsr.php on line 21
Could not run query:


I'm suspecting it's to do with the contents of the table, things not quite matching up from the other end... but if something stands out could you please offer your insights?

Thanks much,

Lionel

Code: Select all

<?php
    $user_name = "ZZZZZ";
    $password = "XXXXXX"; 
    $database_name = "XXXX";
    $table_name = "OIOIO"; 
 
 
 
    $key = $_POST["avikey"];
    $full_name = $_POST["full_name"];
 
 
    $dbh = mysql_connect("localhost", $user_name, $password, $database_name);
    if (!$dbh)
    {
        die("Not connected : " . mysql_error($dbh));
    }
    else
    {
        $query = "SELECT * FROM `$table_name` WHERE aviname = '$key'";
        $result = mysql_query($dbh,$query);
        if (!$result)
        {
            echo 'Could not run query: ' . mysql_error($dbh);
        }
        else
        {
            $row = mysql_fetch_row($result);
 
            if ($row >= 1)
            {
                echo ("key_in_list");
            }
            else
            {
                $query = "
                        INSERT INTO `$database_name`.`$table_name` (
                        `avikey`,
                        `full_name`
                        )
                        VALUES (
                        '$key', '$full_name')";
                $result = mysql_query($dbh,$query);
                echo ("new_key_added");
            }
        }
        mysql_close($dbh);
    }
?>
 
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: SQL: mysqli doesn't work, but neither does mysql_ in part

Post by jaoudestudios »

You can use mysqli, the error is not saying there is something wrong with your api but your query is incorrect.

Please post your schema.

Also echo out your query once it has been populated with the variables as some of the variables could be coming through empty which would cause an error.

NB: You will need protection on your queries from mysql injection if you wish to keep your data.
Post Reply