Connect to and use mysql database


#1

Hi,

I’m trying to connect to my database in a php file, with this code:

<?php $con = mysql_connect('epiz_(all numbers)_oil', 'root', ''); ?>

To test that this works, I’m using this code:

<?php $query = mysql_query("SELECT * FROM search_product"); $num_rows = mysql_num_rows($query); echo $num_rows; ?>

Do anyone know why this doesn’t work and how to fix it?

Thanks!


#2

try following code

<?php $servername = "*****"; $username = "*****"; $password = "*****"; $dbname = "******"; $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error()); if (mysqli_connect_errno()) { printf("Connect failed: %s\ ", mysqli_connect_error()); exit(); } ?>

#3

A few tips:

  • mysql_connect is outdated and should not be used anymore. Please switch to MySQLi or PDO immediately.
  • Try checking the error message returned by the MySQL function.
  • Please make sure you use all the right database settings, not just the right database name.

#4