How to display stored url in database to a webpage? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to PHP and MySQL. I've created a form that uploads a url to my database field and I would like to know how to display the stored url from the database to a webpage.

Like this
$query = "SELECT url FROM table";
$resource = mysql_query($query);
if (mysql_num_rows($resource) > 0) {
$results = array();
while($record = mysql_fetch_array($resource)) {
$results[] = $record;
}
}
foreach ($results as $result) {
echo $result['url'];
}
And if you are among the mysql_* complain group:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT url FROM table";
if ($result = $mysqli->query($query)) {
// Loop through results
$results = array();
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
$result->free();
}
foreach ($results as $r) {
echo $r['url'];
}
$mysqli->close();
Edit I would like to add that it would not be a bad idea to first consult a beginners PHP/MySQL book and get yourself up to speed with the programming basics.
Resource
PHP Beginner books

As we don't know your database structure or the used PHP library, I assume that you can select the stored information into an array like this:
$row['URL'] = 'your stored url';
Then you can easily print it out using more formulas:
<?php print('Click here'); ?>
Or if you want to integrate php only for the url printing:
Click here
Basically the two is identical, but if you have a large HTML and minimal PHP, then the second solution would be easier to edit and maintain.

Related

PHP (If not existant in records.) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So basically I got this code right here:
<?php
include_once 'dbconfig2.php';
$con = new DB_con();
$table = "users";
if(isset($_GET['profile_id']))
{
$sql=mysql_query("SELECT * FROM users WHERE user_id=".$_GET['profile_id']);
$result=mysql_fetch_array($sql);
}
?>
I am clueless as to how I would make it so if the user_id is not existent in the records, they cannot view their profile but it leads them to another messsage or piece of code.
If the user_id doesn't exist, there won't be any rows in the result. When you try to read a row with mysql_fetch_array(), it returns FALSE. So you can simply test $result:
if (!$result) {
die("Invalid profile ID");
}
Try to use prepared statements using mysqli, in order to avoid sql injection.
By way of example:
$mysqli = new mysqli("localhost", "root", "root", "test");
if ($mysqli->connect_errno) {
echo "connect_error". $mysqli->connect_error;
}
$id = $_GET['profile_id'];
$result = $mysqli->prepare('SELECT name FROM users WHERE user_id = ?');
$result->bind_param("i", $id);
$result->execute();
$result->bind_result($col1);
$result->fetch();
$is_valid_profile = (!$col1) ? 'Invalid profile' : 'Valid profile';
echo $is_valid_profile;
$result->close();
http://php.net/manual/en/mysqli.prepare.php

Php execute lines [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to execute these lines When I click on the button:
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
It will pick another word from the database using that query.
The button in the HTML page:
echo "<A HREF=$self?n=$n>Play again.</A>\n\n";
How can I do this?
To actually run the query you can use MySQLi. Here is a good beginners guide.
//Connect to database.
$db = new mysqli('localhost', 'user', 'pass', 'database');
//The code you provided.
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
//Run the query.
$result = $db->query($sql);
//Get the first (and presumably only) row
$row = $result->fetch_assoc();
//Output the word.
echo $row['word'];
//Free up some memory.
$result->free();
To get the link to work, you need to have the name of the page. You can either hardcode it, or use $_SERVER['PHP_SELF']:
echo 'Play again!'
(I don't know what you use the $n for so I just left it in there.)

Render a HTML page using MySQL (Dynamic lines) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am wondering how I would render a HTML page with MySQL.
I want to render a page that has different lines, for different rows in the table.
The table is:
I want the web page to render like this:
Obviously, if there are less rows I don't want as many rows to display, and same if there are more.
Purely because it's late at night here's some free code.
Please notice two things.
1. I'm using mysqli not mysql.
2. I wrote this from memory, I researched on the web how to write this. I have no degree, no professional job as a programmer but I all found this out by myself by researching and browsing SO. Please next time research and try out different things for yourself.
<?php
$host = 'localhost';
$password = '';
$user = '';
$database = '';
// Link for the connection to MySQL
$link = mysqli_connect($host,$user,$pass);
// If statement to check if the link has been succesful if not, give error. If it is succesful select database.
if(!$link)
{
if (mysqli_connect_errno())
{
echo "Connection unsuccessful<br/>";
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}else
{
mysqli_select_db($link, $database);
}
$query = "SELECT * FROM [table]";
$result = mysqli_query($link, $query);
echo '<table>';
while($row = mysqli_fetch_assoc($result))
{
echo '<tr>';
echo '<td>'. $row['CSTEAMID'] .'</td>'
echo '<td>'. $row['OSTEAMID'] .'</td>'
echo '<td>'. $row['TIMEJOINED'] .'</td>'
echo '</tr>';
}
echo '</table>';
?>
Do you have any code already?
Pseudo code for what you want:
Write html for a standard page
In the body somewhere, use PHP (or other scripting language) to connect to your mysql db and retrieve the needed data.
Output with the PHP in an HTML tabular format

query for fetching only one integer value using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
hey i just want a query which will be printing only the result of count query . put in the else part
<?php
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('quiz', $con) or die(mysql_error());
$q="count(*) from question where ans=uanswer";
$rq=mysql_query($q,$con);
if(!$rq)
{
echo " the sql query faiiled to work ";
}
else
{
}
?>
You would use the function mysql_fetch_row() to return a row from your database query as an array. You can then access the result for the count(*) as the first element in the returned array.
<?php
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('quiz', $con) or die(mysql_error());
$q="count(*) from question where ans=uanswer";
$rq=mysql_query($q,$con);
if(!$rq)
{
echo " the sql query faiiled to work ";
}
else
{
$row = mysql_fetch_row($rq);
echo $row[0];
}
?>

Two fields in one field [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am not experienced in PHP so I need your help on this. I would like to create on array from two, if I can say like that.
Basically, I would like to make from this:
[{"Naziv":"Nemanja","duzina":"45.475","sirina":"23.423"},{"Naziv":"Aleksandar","duzina":"45.427","sirina":"21.124"}]
to something like this:
[{"Naziv":"Nemanja","duzina":["45.475","23.423"]},{"Naziv":"Aleksandar","duzina":["45.427","21.124"]}]
Here is my PHP:
// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);
// Check connection
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
// query the application data
$sql = "SELECT Naziv,duzina,sirina FROM lokacije";
$result = mysqli_query($con, $sql);
// an array to save the application data
$rows = array();
// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
// close the database connection
mysqli_close($con);
// echo the application data in json format
echo json_encode($rows);
Instead of
$rows[] = $row;
create a new array in your desired format:
$rows[] = array(
'Naziv' => $row['Naziv'],
'duzina' => array($row['duzina'], $row['sirina'])
);

Categories