Getting error "No database selected" [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Trying to query database table. I'm getting a connection successful result when I run the connection script but can't figure out whats wrong with the query. Sorry I'm new to php
<?php
$servername = "localhost";
$database = "laravel";
$username = "root";
$password = "root";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$result = mysql_query("SELECT * FROM users WHERE id=1") or die(mysql_error());
if(mysql_num_rows($result) > 0): ?>
<table>
<tr>
<th>name</th>
<th>lastname</th>
<tr>
<?php while($row = mysql_fetch_assoc($result)): ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['lastname']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<?php endif; ?>
?>

No, don't MIX them, just use PDO all the way through. Use methods ->query() and ->fetch() accordingly.
<?php
$servername = "localhost";
$database = "laravel";
$username = "root";
$password = "root";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to exception
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$query = $conn->query('SELECT * FROM users');
?>
<table>
<tr>
<th>name</th>
<th>lastname</th>
<tr>
<?php while($row = $query->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['lastname']; ?></td>
</tr>
<?php endwhile; ?>
</table>

Related

Displaying columns in PHP [duplicate]

This question already has answers here:
Show values from a MySQL database table inside a HTML table on a webpage
(9 answers)
Closed 1 year ago.
Would anyone know how to get this result ? Basically I just need to display the columns as well.
Wanted Result image
Current result image
<h1>
Harry Potter Movies
</h1>
<?php
$hostname = "localhost";
$dbUser = "root";
$dbPassword = "root";
$dbName = "harry potter movies";
$port = 3306;
$conn = mysqli_connect($hostname, $dbUser, $dbPassword, $dbName, $port);
$sql = "SELECT * FROM Movies";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["movie"]. " " . $row["rating"]. " " . $row["year"]. "<br>" ;
}
} else {
echo "0 results";
}
?>
You can use a table for that.
<?php
$hostname = "localhost";
$dbUser = "root";
$dbPassword = "root";
$dbName = "harry potter movies";
$port = 3306;
$conn = mysqli_connect($hostname, $dbUser, $dbPassword, $dbName, $port);
if (mysqli_connect_errno()) exit("Failed to connect to MySQL: " . mysqli_connect_error());
?>
<h1>Harry Potter Movies</h1>
<table>
<thead>
<tr>
<th>Movie</th>
<th>Rading</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<?php
$result = $conn->query("SELECT movie, rating, year FROM Movies");
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?= htmlentities($row["movie"], ENT_QUOTES) ?></td>
<td><?= htmlentities($row["rating"], ENT_QUOTES) ?></td>
<td><?= htmlentities($row["year"], ENT_QUOTES) ?></td>
</tr>
<?php
}
} else { ?>
<tr>
<td colspan="3">0 results</td>
</tr>
<?php
}
?>
</tbody>
</table>
What you want is something like this:
<table>
<tr>
<th>
Movie
</th>
<th>
Rating
</th>
<th>
Year
</th>
</tr>
<tr>
<td>
Harry Potter and the Philosopher's Stone
</td>
<td>
7.6
</td>
<td>
2001
</td>
</tr>
So your echo should ouptup some HTML tags with the content.

Change code from mysql to mssql in a php file

I'm new to php and sql.
The codes below are written in php and working fine when connecting to a mysql database. SELECT query is working and UPDATE query is working.
But when connecting to an mssql database, the codes don't work well.
I need to convert them to connect to a similar mssql database.
Thank you.
<table id="data_table" class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Designation</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
$sql_query = "SELECT id, firstname, lastname, address, email FROM
myguests LIMIT 10";
$resultset = mysqli_query($conn, $sql_query) or die("database
error:". mysqli_error($conn));
while( $developer = mysqli_fetch_assoc($resultset) ) {
?>
<tr id="<?php echo $developer ['id']; ?>">
<td><?php echo $developer ['id']; ?></td>
<td><?php echo $developer ['firstname']; ?></td>
<td><?php echo $developer ['lastname']; ?></td>
<td><?php echo $developer ['address']; ?></td>
<td><?php echo $developer ['email']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
/* Database connection start */
$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\n", mysqli_connect_error());
exit();
}
?>
<?php
include_once("db_connect.php");
$input = filter_input_array(INPUT_POST);
if ($input['action'] == 'edit') {
$update_field='';
if(isset($input['firstname'])) {
$update_field.= "firstname='".$input['firstname']."'";
} else if(isset($input['lastname'])) {
$update_field.= "lastname='".$input['lastname']."'";
} else if(isset($input['address'])) {
$update_field.= "address='".$input['address']."'";
} else if(isset($input['email'])) {
$update_field.= "email='".$input['email']."'";
}
if($update_field && $input['id']) {
$sql_query = "UPDATE myguests SET $update_field WHERE id='" .
$input['id'] . "'";
mysqli_query($conn, $sql_query) or die("database error:".
mysqli_error($conn));
}
}
The rough equivalent of MySQL LIMIT in SQL Server is TOP, so you may try something like:
SELECT TOP 10 id, firstname, lastname, address, email
FROM myguests
ORDER BY <some_column>;
Note carefully that I added an ORDER BY clause to your query. Using LIMIT (or TOP) without ORDER BY is fairly meaningless, because you haven't told SQL which 10 rows you want, relative to some ordering.

retrieve data from mysql and display it in a table using php

I am trying to retrieve all data from my database and displaying it in a table. But i could not do that, am facing some problem.I am getting error as,
This page isn’t working.
localhost is currently unable to handle this request.
Here is my code,
<html>
<body>
<table style="width:100%">
<tr>
<th>Driverid</th>
<th>Truckid</th>
<th>Imagecount</th>
<th>Trainingstatus</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "IDdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT DriverID, TruckID, Imagecount, Trainingstatus FROM IDs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
</table>
<?php}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
please replace your code
from
driverid = $row["Driverid"];
truckid = $row["Truckid"];
imagecount = $row["Imagecount"];
trainingstatus = $row["Trainingstatus"];
to
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];
please reader the section about how to define variables in php on the PHP Manual ,after that, check the variables defined in your code.
you have not started table tag, also not in loop, and main part is sdd space <?php} to <?php }
if ($result->num_rows > 0) {
echo '<table>';
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
<?php }
echo '</table>';
} else {
echo "0 results";
}

How to search and output with a form input value using PDO

I am trying to perform a search action by getting a value from HTML form using PDO and display the results in the same page. I am getting no errors also no results. Please advise! i am beginner.
<?php
require_once 'db_alternate2.php';
session_start();
if (isset($_POST['submit'])) {
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$payrl_search = trim($_POST['empname_search']);
$sql = "SELECT * FROM payroll_db
WHERE 'pay_staff_name'
LIKE '$%payrl_search%'";
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
}
my php code inside html is
<table>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($r['pay_emp_id'])?></td>
<td><?php echo htmlspecialchars($r['pay_staff_name']); ?></td>
<td><?php echo htmlspecialchars($r['pay_month_sal']); ?></td>
<td><?php echo htmlspecialchars($r['pay_amount']); ?></td>
<td><?php echo htmlspecialchars($r['pay_bankname']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
I am getting these errors:
Undefined variable: q
Fatal error: Call to a member function fetch() on a non-object
i think my error part is
<?php while ($r = $q->fetch()): ?>

Data is not coming from mysql

It is showing that connection has been created. But I don't know what is the problem in query. After that query I tried to echo something and it is visible there in browser.
<html>
<head>
</head>
<body>
<?php
$servername = "localhost:8888";
$username = "root";
$password = "";
$dbname = "employees";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else {
echo "Connected successfully";
}
$res = mysqli_query($conn,"select * from dept_manager");
echo"dept_manager";
while($row=mysqli_fetch_assoc($res))
{
?>
<table>
<tr>
<td><?php echo $row['emp_no'] ?></td>
<td><?php echo $row['dept_no']?></td>
<td><?php echo $row['from_date'] ?></td>
<td><?php echo $row['to_date'] ?></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
$sql = "select * from dept_manager";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
// your code here
}
Try with this..
Try this in the place of while loop:
<?php
$res = mysqli_query($conn,"select * from dept_manager");
echo"dept_manager";
echo "<table>";
while($row=mysqli_fetch_assoc($res))
{
echo "<tr><td>" . $row['emp_no'] . "</td><td>" . $row['dept_no'] . "</td><td>" . $row['from_date'] . "</td><td>" . $row['to_date'] . "</td></tr>";
}
echo "</table>";
?>
You have mentioned servername:localhost:8888 just check once remove
8888 and try.
I have tried with your code in my local system,same code working fine.
Just i have entered my database name and table name,it's working fine.
So you just follow my example and test once.
COde:-
<html>
<head>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydashboard";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
echo "Connected successfully";
}
$res = mysqli_query($conn,"select * from content_values");
while($row=mysqli_fetch_assoc($res))
{
?>
<table>
<tr>
<td><?php echo $row['name'] ?></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
Output:-
Connected successfully
Name:
Test QA AndroidSD
image sampl
GMAIL
test PDF
Nat Geo Video

Categories