i can't seem to run this code under local host, my goal is to make a table that is supposed to show in a website but when i try to connect i get the error:
Warning: mysql_connect(): in C:\xampp\htdocs\PhpProject2\hent.php on line 5 can not connect
my site name: http://localhost/PhpProject2/hent.php
the code:
<html>
<body>
<?php
mysql_connect('<server is here>','<my username here>','<password here>')
or die('can not connect' );
mysql_select_db('<my username here>') or die ('can not connect to <username here>');
$sql = "Select * from Customer";
$result = mysql_query($sql);
$number= mysql_num_rows($result);
for($i=0; $i < $number; $i++)
{
$table = mysql_fetch_row($result);
echo $table[0], $table[1];
echo '<br>';
}
?>
</body>
</html>
i'm using xampp and MySQL is running on port 3306:]
instead of my < username here >, < server is here >, < password here > there is real code :]
i would Appreciate any answer :]
Try this:
<?php
$host = "hostname";
$user = "username";
$password = "password";
$database = "database";
$link = mysqli_connect($host, $user, $password, $database);
If (!$link){
echo ("Unable to connect to database!");
}
else {
$query = "SELECT * FROM Customer";
$result = mysqli_query($link,$query);
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo $row['<insert column name>']. "<br>";
}
}
mysqli_close($link);
?>
I have used the MYSQL library in this code. You should check if you column in mysql is called 0 and 1. B.T.W. I am using WHILE instead of FOR loop that is just a personal preference.
Here is what you can do to see your error:
mysql_connect('<server is here>','<my username here>','<password here>')
or die('Error: '.mysql_error() );
And try to refrain from using all functions starting with mysql_*
. They are currently being depricated.
Use mysqli or pdo
I would suggest you to start using PDO;
index.php
<?php
require "dbc.php";
$getList = $db->getAllData(25);
foreach ($getList as $key=> $row) {
echo $row['columnName'] .' key: '. $key;
}
?>
dbc.php
<?php
class dbc {
public $dbserver = 'server';
public $dbusername = 'user';
public $dbpassword = 'pass';
public $dbname = 'db';
function openDb() {
try {
$db = new PDO('mysql:host=' . $this->dbserver . ';dbname=' . $this->dbname . ';charset=utf8', '' . $this->dbusername . '', '' . $this->dbpassword . '');
} catch (PDOException $e) {
die("error, please try again " . $e);
}
return $db;
}
function getAllData($qty) {
//prepared query to prevent SQL injections
$query = "Select * from Customer where zip= ?";
$stmt = $this->openDb()->prepare($query);
$stmt->bindValue(1, $qty, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
?>
This is how I usually connect to an MySQL database. I have config.php
<?php
function db_connect(){
$conn = new mysqli('localhost', 'root', 'leave_black_if_no_password_set', 'database_name');
if (!$conn) {
return false;
}
$conn->autocommit(TRUE);
return $conn;
}
?>
//end config
Now, in your other file just call config.php: `include 'config.php';
<html>
<body>
<?php
include 'php/config.php';
$dbCon = db_connect();
$sql = "Select * from Customer";
$result = mysqli_query($dbCon, $sql);
$number= mysqli_num_rows($result);
while($row=mysqli_fetch_array($result)){
{
echo $row['column_name'];
echo '<br>';
}
//close conn
mysqli_close($dbCon);
?>
</body>
</html>