Page 1 of 1

mysql_query(): supplied argument is not a valid..?

Posted: Tue Jun 03, 2008 4:14 pm
by josh22x
Hey, I am trying to connect to my database and I keep getting the error:

mysql_query(): supplied argument is not a valid MySQL-Link resource

Below is the the section of the PHP code that I am using to process this form, once the user has logged in:

Code: Select all

<?php
session_start();
error_reporting(E_STRICT);
require_once('required_files.php');
require_once('database_connect.php');
 
$username = $_SESSION['valid_user'];
$title=$_POST['title'];
$content=$_POST['content'];
 
$connect = db_connect();
 
if ($connect)
echo 'works';
else
echo 'Did not connect';
 
$insert = "insert into posts (post_id, username, title, content, input_time) VALUES ('', '".$username."', '".$title."', '".$content."', CURRENT_TIMESTAMP())"; 
  
$con = mysql_query($insert, $connect);
 
if($con){
echo 'Query good';}
else{
echo error_display();}
?>
 
What does this error mean exactly?

Also is the syntax correct for my insert statement? - Why do I have to enclose the variable like this: '".$variable."' - to get them to work? I though they only need to be enclosed within single quotes ('$variable') for the MySQL query?

Thanks so much!

Re: mysql_query(): supplied argument is not a valid..?

Posted: Tue Jun 03, 2008 4:53 pm
by Weirdan
please post the exact error message you received - it includes the file name and line number where error has occurred.

Re: mysql_query(): supplied argument is not a valid..?

Posted: Tue Jun 03, 2008 5:23 pm
by josh22x
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/html/PHP_blog/blog_engine.php on line 20

Here you go, Thanks!

Re: mysql_query(): supplied argument is not a valid..?

Posted: Tue Jun 03, 2008 5:40 pm
by califdon
josh22x wrote:mysql_query(): supplied argument is not a valid MySQL-Link resource

What does this error mean exactly?
What it means exactly is that $con is not a valid MySQL-Link resource. Why not? Because your query failed, so there was no resource to link to. Why didn't you know that it had failed? Because you neglected to use the valuable information available to you in mysql_error(). Always code like this:

Code: Select all

$con = mysql_query($insert, $connect) or die(mysql_error());
Then you will be shown the reason the query failed.
Also is the syntax correct for my insert statement? - Why do I have to enclose the variable like this: '".$variable."' - to get them to work? I though they only need to be enclosed within single quotes ('$variable') for the MySQL query?
Your syntax is valid, but you can also express it without the concatenation, at least in this case, where the total string is enclosed in double quotes. So this would be equally valid:

Code: Select all

$insert = "insert into posts (post_id, username, title, content, input_time) VALUES ('', '$username', '', '$content', CURRENT_TIMESTAMP())";
Many programmers feel that it is safer to always concatenate, so you won't forget to do it when you're creating a string enclosed in single quotes.

Re: mysql_query(): supplied argument is not a valid..?

Posted: Tue Jun 03, 2008 8:41 pm
by josh22x
Ok, thanks for your help!

So I changed it to:

Code: Select all

$con = mysql_query($insert, $connect) or die(mysql_error());
However, I still get the same error, no extra information. What am I doing wrong?

Thanks!

Re: mysql_query(): supplied argument is not a valid..?

Posted: Tue Jun 03, 2008 9:06 pm
by califdon
josh22x wrote:Ok, thanks for your help!

So I changed it to:

Code: Select all

$con = mysql_query($insert, $connect) or die(mysql_error());
However, I still get the same error, no extra information. What am I doing wrong?

Thanks!
I'm not sure. That's surprising to me. Since it's not returning an error pointing to something wrong in the SQL statement, I'm inclined to think that the connection isn't working. I see that you tested for a valid $connect, so I'm not sure what's happening.

I would do several things to try to determine what's going on:

1. I would echo back the value of $insert just before trying to run the query, although as I said, I would expect mysql_error() to point to the problem if the SQL string had a problem.

2. I would insert a simple SELECT statement before your INSERT, to see if you can return any data from the table. Something very simple, like:

Code: Select all

$sql="SELECT * FROM posts LIMIT 1";
$result=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_array($result) or die(mysql_error());
echo $row[0] . " - " . $row[1] . " - " . $row[2];

Re: mysql_query(): supplied argument is not a valid..?

Posted: Tue Jun 03, 2008 9:49 pm
by josh22x
Ok, I have got a more informative error now: Here you go:

Code with your lines added:

Code: Select all

<?php
session_start();
// Report all PHP errors
error_reporting(E_STRICT);
require_once('required_files.php');
require_once('database_connect.php');
 
$username = $_SESSION['valid_user'];
$title=$_POST['title'];
$content=$_POST['content'];
 
$connect = db_connect() ;
 
if ($connect)
echo 'works';
else
echo 'Did not connect';
 
$sql="SELECT * FROM blogs LIMIT 1";
$result=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_array($result) or die(mysql_error());
echo $row[0] . " - " . $row[1] . " - " . $row[2];
 
$insert = "insert into blogs (username, title, content, input_time) VALUES ('$username', '$title', '$content', CURRENT_TIMESTAMP())"; 
  
$con = mysql_query($insert, $connect) or die(mysql_error());
?>
 
Error:


Notice: Undefined index: title in /var/www/html/PHP_blog/blog_engine.php on line 9

Notice: Undefined index: content in /var/www/html/PHP_blog/blog_engine.php on line 10
works
Warning: mysql_query() [function.mysql-query]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/html/PHP_blog/blog_engine.php on line 20

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/html/PHP_blog/blog_engine.php on line 20
Access denied for user 'www-data'@'localhost' (using password: NO)


Also here is my database connect function if you need to see it:

Code: Select all

<?php
 
function db_connect()
{
   $result = new mysqli('localhost', 'tuser', 'password', 'data_posts'); 
   if (!$result)
     throw new Exception('Could not connect to database server');
   else
     return $result;
}
 
?>

Re: mysql_query(): supplied argument is not a valid..?

Posted: Tue Jun 03, 2008 10:31 pm
by dbemowsk
first off, califdon mentioned that $con is not a valid link resource. Well, this is true, but according to the code, it is not meant to be. $con is the query result and is not being used as the link resource identifier. The non-valid link resource would be $connect. In your db_connect() function, you have the line

Code: Select all

$result = new mysqli('localhost', 'tuser', 'password', 'data_posts');
in which i$result is being returned to your $connect variable. $result is the mysqli class object. When the line

Code: Select all

$con = mysql_query($insert, $connect);
is executed, you are telling the query to use the resource identifier supplied by the variable $connect which is then an object and not a link resource identifier.

Try just using

Code: Select all

$con = mysql_query($insert);
By not specifying the resource identifier in the mysql_query explicitly, the query will attempt to use the last link opened by a mysql_connect statement.

OR, Try replacing

Code: Select all

$result = new mysqli('localhost', 'tuser', 'password', 'data_posts');
with

Code: Select all

$result = mysqli_connect('localhost', 'tuser', 'password', 'data_posts');
The mysqli_connect function will then return a valid link resource identifier.

I have not yet used the mysqli extension, so you may run into some other issues using it. If you do you will need to read more on this extension at http://us.php.net/manual/en/book.mysqli.php

Hope that clears some things up.

Re: mysql_query(): supplied argument is not a valid..?

Posted: Wed Jun 04, 2008 12:24 am
by califdon
Thanks, dbemowsk, I misstated that. Josh, follow dbemowsk's advice!