4 Replies - 326 Views - Last Post: 08 April 2017 - 04:30 PM

#1 d4everman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-April 17

Problem connecting to MySQL

Posted 08 April 2017 - 10:31 AM

On page 23 in the book PHP and MySQL I created this script as it is in the book
<?php
#Incorporate the MySQL connection script.
require('../connect_db.php' );
#Display MySQL version and host.
if( mysqli_ping($dbc))
{ echo 'MySQL Server'. mysqli_get_server_info( $dbc ).
	'on' . mysqli_get_host_info( $dbc ) ;}



Yet I get this error message:
# connect on 'localhost' for user 'mike' # with password 'easysteps' to database 'site_db' $dbc = mysqli_connect ( 'localhost' , 'mike' , 'easysteps' , 'site_db' ) OR die (mysqli_connect_error()); #set encoding to match PHP script encoding. mysqli_set_charset( $dbc , 'utf8' );
Notice: Undefined variable: dbc in C:\Abyss Web Server\htdocs\require.php on line 5

Warning: mysqli_ping() expects parameter 1 to be mysqli, null given in C:\Abyss Web Server\htdocs\require.php on line 5

I've double checked the codes on the proceeding pages and I can't determine where the error is coming from. Can anyone with more knowledge help me figure it out?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem connecting to MySQL

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5061
  • View blog
  • Posts: 13,657
  • Joined: 18-April 07

Re: Problem connecting to MySQL

Posted 08 April 2017 - 11:46 AM

Where is the $dbc = mysqli_connect() line located? Is it inside the connect_db.php file? The notice message is telling you that on line 5 that the variable $dbc was not being set. If your mysqli_connect() function is failing to connect, it may not be setting that variable or setting it to null.

Did you install mysql on your local computer? Is it up and running? It has to be running as a service on your computer before you can execute this code.

1) Check MySQL is running on your computer
2) Make sure you have a user account called mike on the running instance
3) Make sure you have a db called site_db
4) Check to make sure that $dbc is being set and that it isn't being jumped over because of another error

Let us know :)
Was This Post Helpful? 0
  • +
  • -

#3 d4everman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-April 17

Re: Problem connecting to MySQL

Posted 08 April 2017 - 01:08 PM

I checked the items you mentioned. The only thing I can determine is that the error is in the connect_db, but I've gone over it and can't find anything wrong. That doesn't mean theres not a syntax error I'm missing.

# connect on 'localhost' for user 'mike'
# with password 'easysteps' to database 'site_db'
$dbc = mysqli_connect
 ( 'localhost' , 'mike' , 'easysteps' , 'site_db' )
 OR die
 (mysqli_connect_error());
 
 #set encoding to match PHP script encoding.
 mysqli_set_charset( $dbc , 'utf8' );



Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5061
  • View blog
  • Posts: 13,657
  • Joined: 18-April 07

Re: Problem connecting to MySQL

Posted 08 April 2017 - 03:30 PM

Ok after the line where you call mysqli_connect, put in a line like this...

var_dump($dbc);



Then run the code. You should see an new line of output where it tells you what the value of $dbc is. If it flags this as an error as $dbc being undefined or shows it as null, then you know that mysqli_connect is not connecting properly or that something else is going on here.
Was This Post Helpful? 0
  • +
  • -

#5 d4everman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-April 17

Re: Problem connecting to MySQL

Posted 08 April 2017 - 04:30 PM

Hmmm...it didn't seem to change anything. I appreciate your advice. I think I'm going to have to go back to the start and work through it again. Obviously I've made a mistake somewhere and I need to find out what it was and where I made it by working through it. But thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1