ZandarGlass Posted November 1, 2013 Share Posted November 1, 2013 My apologies if I've placed this in the wrong section. It concerns a basic connection to n SQL database, and doing a simple count of the records.I'm learning from DAVID POWERS PHPSOLOUTIONS book. Examples in his book show connecting to the local db differently than my host wants me to connect, and I believe therin lies the issue. If I can figure out the method or problem I'm having, I'll be very thankful. I'm just connecting to a database, running one query, and counting records. The code that I altered, and I am trying to use, looks like this: <?php//Sample Database Connection Syntax for PHP and MySQL.include ('/includes/imageconn.inc.php');include ('/includes/imagetableconn.inc.php');mysql_select_db($dbname);# Check If Record Exists$sql = "SELECT * FROM $usertable";$result = $conn->query($sql) or die(mysqli_error());$numrows = $result->num_rows;?><html><head><title>PHP Test</title></head><body><?php echo $numrows; ?></body></html> ----------------------------------------------------------------------- My imageconn.inc.php file holds a set of variables needed for the connection. I have verified it works (online, where I want), using other simple pages I made. This is the imagetableconn.inc.php file. It contains one line of code (which also works in the test file the host sent me). $conn = mysql_connect($hostname, $username, $password) or die ('Error connecting to mysql'); ------------------------------------------------------------------------- I'm pretty sure the problem lies in this line (which is from the BOOK, which I can't figure out how to modify for my online needs): $result = $conn->query($sql) or die(mysqli_error()); My error message: Fatal error: Call to a member function query() on a non-object in D:\Hosting\4641474\html\testimagedb1.php on line 12 Quote Link to comment https://forums.phpfreaks.com/topic/283508-basic-phpmysql-code-problem/ Share on other sites More sharing options...
objnoob Posted November 1, 2013 Share Posted November 1, 2013 (edited) You're mixing procedural code with object oriented code! You're also mixing mysql_* with mysqli_* Try to connect with this! $conn = new mysqli($hostname, $username, $password, $dbname); // check if connection attempt returned an error number if(mysqli_connect_errno()){ echo 'Error connecting to database!'; exit; # kill script; couldn't connect to database } Delete this because we're are not using msyql_* we're using mysqli, which is what you want to use! mysql_select_db($dbname); Edited November 1, 2013 by objnoob Quote Link to comment https://forums.phpfreaks.com/topic/283508-basic-phpmysql-code-problem/#findComment-1456502 Share on other sites More sharing options...
ZandarGlass Posted November 1, 2013 Author Share Posted November 1, 2013 (edited) Ah. Got it. Thanks! Edited November 1, 2013 by ZandarGlass Quote Link to comment https://forums.phpfreaks.com/topic/283508-basic-phpmysql-code-problem/#findComment-1456515 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.