bagofmilk bagofmilk - 2 months ago 19
MySQL Question

Cannot select database

I'm trying to display a

SQL
database using
php
.

I get an error:

Cannot Select Database (running local on XAMPP).


In
XAMPP
I went into
xampp-security
and changed the password of the superuser to "thisthing"

I have a file named:
mydb.sql


mydb.sql:

CREATE TABLE `dpuForm` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`JN` TINYTEXT NOT NULL ,
`PN` TINYTEXT NOT NULL ,
`QTY` TINYTEXT NOT NULL ,
`DESC` TINYTEXT NOT NULL ,
PRIMARY KEY (`ID`) );

INSERT INTO 'dpuForm' ('JN',...........yadayadayada


And here's my php:

<?php

$host = "localhost";
$username="root";
$password="thisthing";
$database="mydb";

mysqli_connect($host,$username,$password);
@mysqli_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM dpuForm";
$result = mysqli_query($query);
$num = mysqli_numrows($result);

mysqli_close();

?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<th>Job Number</th>
<th>Part Number</th>
<th>QTY</th>
<th>Description</th>

<?php
$i=0;
while ($i < $num) {
$f1=mysqli_result($result,$i,"JN");
$f2=mysqli_result($result,$i,"PN");
$f3=mysqli_result($result,$i,"QTY");
$f4=mysqli_result($result,$i,"DESC");
?>
<tr>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo $f4; ?></td>
</tr>
<?php $i++;} ?>
</table>
</body>
</html>

Answer

you need to use

$link = mysqli_connect($host,$username,$password,$database);
$result = mysqli_query($link,$query);

This is because you are using the procedural style of mysqli and not the object oriented.

Refer: mysql database

Comments