Seems pretty straightforward, but not getting an error or result.
<html>
<body>
<?php
$con = mysql_connect("localhost","***","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ubook247", $con);
$result = mysql_query("SELECT * FROM buzz_data
WHERE index=4");
while($row = mysql_fetch_array($result))
{
echo $row['buzz_img'] . " " . $row['buzz_title'];
}
?>
</body>
</html>
screenshot of db:
Index is a keyword in SQL, you'll need to escape it for the query to work. Try this:
SELECT * FROM buzz_data WHERE `index` = 4
Try editing the following row:
while($row = mysql_fetch_array($result))
into becoming like this:
while($row = mysql_fetch_assoc($result))
This makes php fetch an array with "labels" for the different fields, instead of naming them 0, 1, 2 and so on.
Related
Good day,
I'm a noob at PHP and MySQl. Looking to be pointed in the right direction.
I have a MySql table with 5 columns. Each column represents a specific set of data. All numerical.
I want to write PHP code which takes each value in a single column and puts it into an array that I can then modify.
I don't know how to get each column as a separate array. Should I get an array of all the rows and then do something extra to separate each of the 5 numbers in each row into 5 separate arrays?
**
require_once 'login.php';
echo $db;
$conn = mysqli_connect($hn,$un,$pw,$db);
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$query = "SELECT xox FROM tablex WHERE id = '1'";
$result = $conn->query($query);
if(!$result) die ("Database access failed: " . $conn->error);
$rows = mysqli_fetch_row($result);
while($rows){
echo $rows['index'];
}
echo $rows[0];
?>
$statement = **yourDatabaseConnection**->query('SELECT column1 FROM table');
$datas = $statement->fetch();
$datas will be an array of your column1 datas
Try reviewing the php documentation, here is a good place to start...php:mysql_fetch_row
Take a look at the example below, if is not what you looking for, please edit your question and provide the approach you are taking (your code) so that we have a better understanding where you are having issues.
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
I have the code bellow, it's ok but I want to be able to use for example the 4th value extracted from the database, use it alone, not put all of them in a list, I want to be able to use the values from database individually. How do I echo them?
Edit: I was thinking to simplify things, to be able to add the values from database into one array and then extract the value I need from the array (for example the 4th - ordered by "order_id"). But how?
Right now I can only create a list with all the values one after the other..
(Sorry, I am new to this). Thank you for all your help..
<?php
include '../../h.inc.php';
$con = mysql_connect($db['host'],$db['user'],$db['passwd']);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$result = mysql_query("SELECT * FROM options WHERE Name LIKE 'x_swift%' ORDER BY order_id");
echo "<table border='1'>
<tr>
<th>Values</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
// echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['VALUE'] . "</td>";
echo "</tr>";
$array = array(mysql_fetch_array($strict));
}
echo "</table>";
mysql_close($con);
?>
To select the value in the value column of the row where order_id is 4, use this SQL:
$query = 'select value from options where order_id = 4';
Then you can access this result in many ways. One is to get the entire result row (which in this case is just one cell) as an associative array:
if ($result = mysql_query($query)) {
$row = mysql_fetch_assoc($result);
echo 'value = ' . $row['value'];
}
You can also get the value directly:
if ($result = mysql_query($query)) {
echo 'value = ' . mysql_result($result, 'value');
}
It would just be a query like...
$result = mysql_query("SELECT * FROM options WHERE ID = 3");
mysql_fetch_row($result);
Unless Im misunderstanding you....let me know
But you really should use PDO, instead of deprecated mysql_* functions
http://php.net/manual/en/book.pdo.php
I am populating data into the dropdown from the Assembly table(the values in the table was added by the user itself using php). Now I want to Assembly_Name in the Part Table. Want to select the value from this drop-down menu and need to insert into the Assembly_Name column of the Part table. I am not able to select the dropdown value and insert it into the Part table.
Part.php
<html>
<body>
<form action="insert_part.php" method="post">
<!--Assembly_Id: <input type="text" name="Assembly_Id">-->
<?PHP
// Connect to your database ** EDIT THIS **
mysql_connect("localhost","root","abc"); // (host, username, password)
// Specify database ** EDIT THIS **
mysql_select_db("test") or die("Unable to select database"); //select db
$result = mysql_query("select assembly_id,assembly_name from assembly ORDER BY Assembly_Id");
echo '<select name="assembly_name"><OPTION>';
echo "Select an option</OPTION>";
while ($row = mysql_fetch_array($result)){
$assembly_name= $row["assembly_name"];
echo "<OPTION value=\"$assembly_name\">$assembly_name</OPTION>";
}
echo '</SELECT>';
?>
Part_name: <input type="text" name="Part_name">
<input type="submit">
</form>
<hr><hr>
<?php
$con = mysql_connect("localhost","abc");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM Part ORDER BY Part_Id");
echo "<table border='1'>
<tr>
<th>Assembly Name</th>
<th>Part Id</th>
<th>Part Name</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Assembly_Name'] ."</td>";
echo "<td>" . $row['Part_Id'] . "</td>";
echo "<td>" . $row['Part_Name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
insert_part.php
<?php
$con = mysql_connect("localhost","abc");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$assembly_name = isset($_POST['assembly_name'])
$sql="INSERT INTO Part (assembly_name,Part_Id, Part_Name) VALUES ('$_POST[assembly_name]','$_POST[Part_Id]','$_POST[Part_name]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header("Location:part.php");
exit;
mysql_close($con);
?>
While Submitting the Value, I am getting the below Error:
Parse error: syntax error, unexpected T_VARIABLE in
C:\wamp\www\insert_part.php on line 10
Step 1: Try to understand what your error is trying to tell you. There is a variable issue on line 10.
Step 2: Check the code for why there could possibly be an issue with a variable in use.
Following this, checking the lines above you will find that the statement on line 9 was not completed with a ; and ran into an "unexpected" variable when reaching line 10
You also do not appear to be submitting a $_POST['Part_Id']
Also, you want quotes in you $_POST such as $_POST['assembly_name'] in your SQL statment.
Line 9 you're missing a semicolon:
$assembly_name = isset($_POST['assembly_name']);
But as #Lion says, do change to using prepared statements. As it is this code is very insecure :(
I want to echo the contents of a table (a product list) yet still have control over the individual fields if possible. While loops did not work as you cannot then go to pick how each field is displayed.
What I am after is to create a product list which then has each record as a hyperlink setting a variable to the product id. Something like:
www.domain.com/catalog/product.php?productid=1
If there isnt a way to echo the fields individually within each row, how would I be able to get this outcome without adding the link into the database itself for all the products?
Its fairly easy
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Product_List");
while($row = mysql_fetch_array($result))
{
echo "<a href='www.domain.com/catalog /product.php?productid=".$row['productid']."'>".$row['productid']."</a>";
echo "<br />";
}
mysql_close($con);
?>
You'll still need an while loop. Consider this:
<?php
$getRows = mysql_query("SELECT id FROM products");
while($row = mysql_fetch_assoc($getRows)) {
echo 'Product link';
}
I've been trying for hours to figure out how to put a link into the following text output via PHP echo(). I basically want to make the title field I'm pulling from my events table (as seen in #4 in the code below) to come back into the browser as a link instead of just text...
the original code that brings back the event title:
<?php
// 3. Perform database Query to bring list of events
$result = mysql_query("SELECT * FROM events", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
// 4. Use returned Data
while ($row = mysql_fetch_array($result)) {
echo $row ["eventtitle"]."<br/>".$row["eventdesc"]."<br/>";
}
?>
How would I go about getting that $row["eventtitle"] to appear in the browser as a link? Let's say if the link was just "eventprofile.php". This is probably an easy fix, but I've been getting a million errors with trying different things with <a href>s.
<?php
$result = mysql_query("SELECT * FROM events", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo "".$row["eventtitle"]."<br/>".$row["eventdesc"]."<br/>";
}
?>
add an anchor tag for it!!
echo "" . $row['eventtitle'] . '' . "<br />" .$row["eventdesc"]."<br/>";
And thats it.