Jump to content


Basic PHP/MySQL Code problem

sql php noob object phpsolutions

  • Please log in to reply
2 replies to this topic

#1 ZandarGlass

  • Members
  • PipPip
  • Member
  • 12 posts

Posted 01 November 2013 - 04:05 PM

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:


//Sample Database Connection Syntax for PHP and MySQL.
include ('/includes/imageconn.inc.php');
include ('/includes/imagetableconn.inc.php');


# Check If Record Exists

$sql = "SELECT * FROM $usertable";

$result = $conn->query($sql) or die(mysqli_error());

$numrows = $result->num_rows;

<title>PHP Test</title>
<?php echo $numrows; ?>



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

#2 objnoob

  • Members
  • PipPipPip
  • Advanced Member
  • 332 posts

Posted 01 November 2013 - 04:17 PM

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
     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! 


Edited by objnoob, 01 November 2013 - 04:21 PM.

#3 ZandarGlass

  • Members
  • PipPip
  • Member
  • 12 posts

Posted 01 November 2013 - 05:08 PM

Ah. Got it. Thanks!

Edited by ZandarGlass, 01 November 2013 - 05:09 PM.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users