0

I have a question. This is a simple piece of php that should show the number of rows in a table.

<?php

$link = mysql_connect("localhost", "root", "1234") or die("Couldn't connect");
mysql_select_db("regulas", $link);

$result = mysql_query("SELECT * FROM index", $link) or die("Couldn't finish query");
$num_rows = mysql_num_rows($result);

echo $num_rows;

?>

However, the page displays:

Couldn't connect

I'm using a xampp server on my own computer: localhost

What am i doing wrong?

Thx Jules

6
  • 1
    mysql_* functions have been removed from PHP 7, and deprecated in previous versions. It's recommend that you switch to PDO or mysqli. Other than that, check for mysql_errors, that will tell you why. Commented Oct 6, 2016 at 16:58
  • What you are doing wrong? Firstly you apparently do not monitor your http servers error log file which is where you can simply read what the issue is instead of having to guess. Commented Oct 6, 2016 at 17:00
  • 3
    As index is a reserved word in MySQL, it has to be enclosed in backticks to not produce a syntax error in this place. Commented Oct 6, 2016 at 17:08
  • I have another script with mysqli but that doesn't work neither Commented Oct 6, 2016 at 17:11
  • Il will just check the log files Commented Oct 6, 2016 at 17:11

2 Answers 2

1

You must provide the password in,there is no defauls password in WAMP or XAAXMP try this :

    <?php

$link = mysql_connect("localhost", "root","", "1234") or die("Couldn't   connect");
mysql_select_db("regulas", $link);

$result = mysql_query("SELECT * FROM index", $link) or die("Couldn't finish query");
$num_rows = mysql_num_rows($result);

echo $num_rows;

?>

Read the manual here : http://php.net/manual/en/function.mysqli-connect.php

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

1 Comment

but i have set a password
0

I changed mysql to mysqli and changed index to siteIndex

<?php

$link = mysqli_connect("localhost", "root", "102030!!", "regulas") or die("Couldn't connect");

$result = mysqli_query($link, "SELECT * FROM siteIndex") or die("Couldn't finish query"); $num_rows = mysqli_num_rows($result);

echo $num_rows;

?>

Comments

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.