0

I must be doing something wrong

I have a very simple script and a very simple database No idea why it's not working..

Please help

<?php

error_reporting(E_ALL);

$link = mysql_connect('localhost', 'root', 'password');
if(!$link)
{
    die('Could not connect: ' . mysql_error());
}

$database = mysql_select_db('test_db', $link);
if(!$database)
{
    die('Could not connect to database: ' . mysql_error());
}

$result = mysqli_query($link, "SELECT forename FROM users WHERE id='1'");
if(!$result)
    echo 'PROBLEM';
$row = mysqli_fetch_array($result);
echo $row[0];

?>

It's not even giving any errors, just echoing 'Problem'...

The database connects fine, and there is 1 user in the database with an ID of 1 and forename is Cristian.

2
  • 7
    Try echo mysql_error(); instead of echo 'PROBLEM'; to get a meaningful message instead of just PROBLEM. Commented Oct 20, 2011 at 1:32
  • this may not relate at all but i would suggest not mixing mysqli and mysql calls together, if you have access to mysqli - use it for all :) Commented Oct 20, 2011 at 1:43

1 Answer 1

5

In $result = mysqli_query try just useing $result = mysql_query same in $row = mysqli_fetch_array should help

Try replaceing you code from $result onward with this

$result = mysql_query("SELECT forename FROM users WHERE id='1'");
if(!$result)
    echo 'PROBLEM';
$row = mysql_fetch_array($result);
echo $row[0];

?>
Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain your reasoning?
This is the correct answer. To elaborate, the OP is connecting using the mysql_ functions, but he is querying and fetching using the mysqli_ functions. The mysqli_ functions are completely unaware of the connection made by mysql_ functions. The solution is to only use mysql_ or mysqli_ functions. They can't be swapped back and forth.
The above answer does not work, it still echoes PROBLEM. Changing problem to mysql_error outputs no error. As for mixing mysql and mysqli functions, I have done so in the past and have encountered no problem, although i will change that now. I am thinking there might be a problem with php reading from the database? Any more ideas?
Alright guys it's fixed. Removed the mysql_select_database and added it to the new mysqli_connect('localhost','root','password','users'). Now all the commands are using mysqli instead of mysql. However the error isn't working. How does mysql_error() work for mysqli? I can get the error for the $link with mysqli_errno() but what about for others? Thanks again for all your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.