<?php
$db_host = "127.0.0.1";
$db_user = "root";
$db_passwd = "111111";
$db_name = "proj_manager";
$mysqli = new mysqli($db_host, $db_user, $db_passwd, $db_name);
if($mysqli) {
echo "connect <br/>";
} else {
echo "not connect <br/>";
}
$query = "show databases;";
$result = mysqli_query($mysqli, $query);
if(!$result) {
die('error:' . mysqli_error());
}
echo "database list : <br/>";
while($row = mysqli_fetch_array($result)) {
echo $row[0] . "<br/>";
}
mysqli_close($mysqli);
?>
I making WebSever by PHP, But I have some problem.
I must saved PHP files at htdocs folder, and my Apache port num is 8081.
(MySQL number is 3307)
So I connected at http://localhost:8081/connect.php
But show this error 404 Not found
Not Found
The requested URL /connect.php was not found on this server.
(http://localhost:8081/phpmyadmin/index.php work perfectly)
How do I solve the problem?
Related
I'am setting up a new wampserver and created a new database with a table named 'users'
apache 2.4.23 & PHP 7.1.10 & mySQL 5.7.14
<?php
$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';
$connection = mysqli_connect($server,$serverUsername,$serverPassword);
msqli_select_db($dbName);
if(!$connection){
echo 'connection failed to database '.mysqli_connect_error();
}
$sql = "SELECT * FROM users";
$query = mysqli_query($sql);
while($row = mysqli_fetch_array($query)){
print_r($row);
}
?>
my value is really in th data base but nothing appears in the page after running code
Have a look at the comments mentioning the fix
$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';
// FIX 1
// You need to mention the database name as the last argument
$connection = mysqli_connect($server,$serverUsername,$serverPassword, $dbName);
if(!$connection){
echo 'connection failed to database '.mysqli_connect_error();
}
$sql = "SELECT * FROM users";
// FIX 2
// The first argument should be your mysqli connection
$query = mysqli_query($connection, $sql);
// Check for errors
if (!$query) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
while($row = mysqli_fetch_array($query)){
print_r($row);
}
?>
Reference for mysqli_connect: https://secure.php.net/manual/en/function.mysqli-connect.php
Reference for mysqli_query: https://secure.php.net/manual/en/mysqli.query.php
I am simply trying to display data from a table in my database.
For some reason, it keeps rendering a blank page.
I have no training at all. SO I am sure my code looks like crap. But I can usually get it to work. I researched but all I could find were connection problems. I get a 'connected successfully' message but no data and no error message.
<?php
$db_host = 'localhost';
$db_user = 'user';
$db_pass = '';
$db_name = '';
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$query = "SELECT * FROM `my_table`";
$result = mysql_query("SHOW COLUMNS FROM `my_table`");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
mssql_field_name($result,0);
// print_r($row);
}
}
?>
$db_name = ' ' ; is Null write Your Database Name
You should first provide a $db_name, because it's currently blank
$db_pass = '';
you need to provide your database name in here, else your application is literally trying to connect to nothing
You might have to check $mysqli_connect, that the $db_name=''; is null...put your database name inside ''
try like this.
<?php
$conn=mysqli_connect('localhost','root',' ','databasename');
$query="Select * from my_table";
$result = mysqli_query($conn,$query);?>
<table>
<tr>
<th>name</th>
<th>full_name</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["full_name"]; ?></td>
</tr>
<?php
}
?>
Please add your database name and database password, then you can try like this :
$sql = "Select * from table_name";
if ($result=mysqli_query($conn,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$response[] = $row;
}
} else {
echo 'Could not run query: ' . mysqli_error();
exit;
}
print_r($response);
I have no idea what's happening.
So on local host, everything loads perfectly fine:
Localhost
and this is the code for connecting:
$servername = 'localhost';
$username = 'root';
$dbname = 'x';
$password = '';
and on the webserver, it does not:
Webserver
and this is the code for the webserver:
$servername = 'localhost';
$username = 'parawtme_root';
$dbname = 'parawtme_xx';
$password = 'password_goes_here';
This is the only different bit in the code. Literally. What am I doing wrong?
My fetch script:
<?php
$sql = "$getdb
WHERE $tablenames.$pricename LIKE 'M9%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo $tableformat;
while($row = $result->fetch_assoc()) {
include 'var.php';
echo $dbtable;
}
echo "</table>";
} else {
echo "0 results";
}
?>
Could it be do to a difference in different PHP servers?
Thanks.
Check connection error and mysqli query error
<?php
$servername = 'localhost';
$username = 'parawtme_root';
$dbname = 'parawtme_xx';
$password = 'password_goes_here';
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "$getdb
WHERE $tablenames.$pricename LIKE 'M9%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo $tableformat;
while($row = $result->fetch_assoc()) {
include 'var.php';
echo $dbtable;
}
echo "</table>";
} else {
echo "0 results "; print_r(mysqli_error($conn));die;
}
?>
i am new to PHP, and i am attempting to create a simple connection from html to mySQL using php. I met with some problems when running my codes.
this is my code:
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT username FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["userid"]. ;
}
} else {
echo "0 results";
}
$conn->close();
?>
after running on a browser, this is displayed:
connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT username FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "
id: ". $row["userid"]. ; } } else { echo "0 results"; } $conn->close(); ?>
Do you have mysql running on your localhost machine? You must verify that it is working first before you can connect via php. Also, make sure you have TCP/IP sockets open in mysql and to make sure it isn't just listening via unix sockets.
echo "<br> id: ". $row["userid"]. ;
this is a syntax error, no need to end it with a full stop.also the correct syntax to connect to sql server is
mysqli_connect("localhost","my_user","my_password","my_db") or die("");
Debug the code before posting it here.
maybe try testing it with a try catch statement. Its what I've done. this way you can display your error messages a little more nicely. As for the cause, PressingOnAlways is probably right
If you are using wampserver or any other server you need to put your php files into c:\wamp\www folder and run them in browser by typing localhost/nameofyourfile.php (change c:\wapm\www for your server installation path or type)
you have the syntax error in blow line
echo "<br> id: ". $row["userid"]. ;
. (dot) should not be there.
second you fetch usernamestrong text by following query
$sql = "SELECT username FROM users";
but you try to get userid
echo "<br> id: ". $row["userid"]. ;
try below code.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "databasename";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT usersname FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["usersname"] ;
}
} else {
echo "0 results";
}
$conn->close();
?>
I have a php script that is supposed to execute several mysql statements, everything works if there are no comments /* */ and no breaklines...
Could you please help me add this functionality, also ignore -- comments
<?
$sqlFileToExecute = 'mysql_dump.sql';
$hostname = 'localhost';
$db_user = 'root';
$db_password = '';
$database_name = 'db_';
$link = mysql_connect($hostname, $db_user, $db_password);
if (!$link) {
die ("error connecting MySQL");
}
mysql_select_db($database_name, $link) or die ("wrong DB");
$f = fopen($sqlFileToExecute,"r+");
$sqlFile = fread($f, filesize($sqlFileToExecute));
$sqlArray = explode(';',$sqlFile);
foreach ($sqlArray as $stmt) {
//THIS SEEMS NOT TO WORK
if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') {
$result = mysql_query($stmt);
if (!$result) {
$sqlErrorCode = mysql_errno();
$sqlErrorText = mysql_error();
$sqlStmt = $stmt;
break;
}
}
}
if ($sqlErrorCode == 0) {
echo "SETUP COMPLETED ;)";
} else {
echo "FAIL!<br/>";
echo "Error code: $sqlErrorCode<br/>";
echo "Error text: $sqlErrorText<br/>";
echo "Statement:<br/> $sqlStmt<br/>";
}
?>
Are you sure you need to run it this manner:
You can also use
$mysql -pxxx -u username db_name -vvv < sourcefile.sql >/tmp/outfile.log
Enable verbose mode to check the stats, else you can omit -vvv option
OR
mysql > source "sourcefile.sql"
You can see more options here