I'm callling php function in html code. But connection between mysql and php is failed.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>go to main page</h1>
<?php actorList()?>
</body>
</html>
<?php
$conn = mysqli_connect("localhost", "root", "*","final");
if(!$conn) die("Connection failed!");
function actorList(){
global $conn;
$result = mysqli_query($conn, "SELECT * FROM actor_list");
}
?>
Why I cannot use $conn in actorList?
Why not pass into a param instead?
<?php
$conn = mysqli_connect("localhost", "root", "qotktk12","final");
if(!$conn) die("Connection failed!");
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>go to main page</h1>
<?php actorList($conn)?>
</body>
</html>
<?php
function actorList($conn){
$result = mysqli_query($conn, "SELECT * FROM actor_list");
}
?>
Or this way will work
<?php
$conn = mysqli_connect("localhost", "root", "","test");
if(!$conn) die("Connection failed!");
function actorList(){
global $conn;
$result = mysqli_query($conn, "SELECT * FROM actor_list");
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>go to main page</h1>
<?php actorList()?>
</body>
</html>
Related
I'm trying to use $_GET to call a dynamic page, but it throws "Error". Do I need to call page 1 from page 2 or why can't they talk to each other?
Thanks in advance for your help, supposingly there's an easy solution.
/Jimmy
This is page 1:
<!DOCTYPE html>
<html>
<head>
<title>Players</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<?php include ("header.php"); ?>
<?php
$servername = "localhost";
$username = "jim";
$password = "pass";
$dbname = "jim";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL search
$sql = "SELECT PlayerID, Person FROM People where PlayerID is not null ORDER BY Person";
$result = $conn->query($sql);
// Condition
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Player</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>"."<a href='spelarfakta.php?=id{$row['PlayerID']}'>{$row['PlayerID']}</a>"."</td>"."<td>".$row["Person"]."</td>";
}
echo "</table>";
} else {
echo "No hits";
}
$conn->close();
?>
</body>
</html>
This is page 2:
<!DOCTYPE html>
<html>
<head>
<title>Player Stats</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<?php include ("header.php");
if (isset($_GET['id'])) {
echo '<p>'. $_GET['id'].'</p>';}
else {echo 'Error';
}
?>
</body>
</html>
Correct solution as a comment:
=id should be id=
I want to make a page where the element will redirect the user to a random page from the database.
I made the HTML code, but I can not deal with PHP and MySQL - I was able to connect to the database, but any attempt to connect the MySQL code with the tag ended in a loss.
Can I count on help writing a PHP script and linking code with ?
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat+Subrayada" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Kaushan+Script" rel="stylesheet">
<title>Random it</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header></header>
<div class="random">
Random
</div>
<?php
require_once "connect.php";
$conn = #new mysqli($host, $db_user, $db_password, $db_name);
$sql = "SELECT link FROM randomit ORDER BY RAND()";
?>
<footer></footer>
</body>
</html>
Let's try this:
Put this PHP part at the top of the page;
<?php
// set a default link in case something goes wrong
$DEFAULT_LINK = "default.html";
// connect to DB
require_once "connect.php";
$conn = new mysqli($host, $db_user, $db_password, $db_name);
// set query -> get 1 result only with LIMIT 1
$sql = "SELECT link FROM randomit ORDER BY RAND() LIMIT 1";
$result = mysqli_query($conn, $sql);
if ($result !== false)
$row = mysqli_fetch_assoc($result);
else
$row = false;
// if we have a valid result => use it
// else => use the default
if ($row && isset($row["link"]))
$RANDOM_LINK = $row["link"];
else
$RANDOM_LINK = $DEFAULT_LINK;
?>
And now the HTML part:
<!DOCTYPE html>
<html>
<!-- head part... -->
<body>
<header></header>
<div class="random">
Random
</div>
<footer></footer>
<!-- etc... -->
So i made a database in my phyMyAdmin and the time i encoded it with my html file and open it in chrome...it says that
Table 'forum.form_tabl' doesn't exist
what happened and what should i do?
this is the code
<?php
session_start();
require"db_connect.php";
$sql= "SELECT forum_id,forum_name FROM form_tabl";
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div id="container">
<table>
<?php if($query->num_rows !==0);
while($row = $query->fetch()):
?>
<tr><td><?php echo $f_name;?>
</td></tr>
<?php endwhile;T_ENDIF;
?>
</table>
</div>
</body>
</html>[enter image description here][1]
//THE OTHER .PHP file is db_connect.php where i put the actual one
<?php
$db = mysqli_connect("localhost","root","","forum") or die("ERROR! With Connection")
?>
enter image description here
$con = mysqli_connect("HostName","UserName","password","DBName") or die("Some error occurred during connection " . mysqli_error($con));
// Write query
$strSQL = "SELECT username FROM MyTable";
// Execute the query.
$query = mysqli_query($con, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo $result["username"]."
";
}
// Close the connection
mysqli_close($con);
?>
How to take out the anchor element from echo (functions.php) and place it to body (index.php)?
PS: Newbie here guys, just trying to understand about HTML, CSS, Javascript and PHP about 2 weeks ago.. just imagine what i learned so far, please don't make it difficult to understand.. just write it :-)
Thanks.
<!--index.php-->
<!doctype html>
<?php include ("functions/functions.php"); ?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?php getCats(); ?>
</ul>
</body>
</html>
//functions.php
<?php
$con = mysqli_connect("localhost","root","","justLearning");
if (mysqli_connect_errno())
{
echo "The connection was not established: " . mysqli_connect_error();
}
function getCats(){
global $con;
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats)){
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo "<a href='index.php?cat=$cat_id'>$cat_title</a>";
}
}
?>
You forgot <li></li> in your code
You need to learn to return an array and to use foreach():
<!doctype html>
<?php include_once("functions/functions.php"); ?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?
$cats = getCats();
foreach($cats as $cat){
echo "<li><a href='index.php?cat=$cat[cat_id]'>$cat[cat_title]</a></li>";
}
?>
<ul>
</body>
</html>
function getCats(){
global $con;
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats)){
$cats[]=$row_cats;
}
return $cats;
}
I guess you can try something like this, if you really want to move the < a > elements to the body.However I think that the way you did it was better (there is no reason to move them to the body):
<!--index.php-->
<!doctype html>
<?php include_once("functions/functions.php"); ?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats))
{
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
?>
<a href='index.php?cat=<?php echo $cat_id;?>'><?php echo $cat_title;?></a>
<?php
}
?>
</ul>
</body>
And in functions.php you just need to leave the mysql connection object:
$con = mysqli_connect("localhost","root","","justLearning");
if (mysqli_connect_errno())
{
echo "The connection was not established: " . mysqli_connect_error();
}
I'm guessing here but I reckon the problem is that the includes_path is set to the default and the included file isn't being found???
<?php
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/functions/' );
include_once( "functions.php" );
?>
<!--index.php-->
<!doctype html>
<html>
<head>
<title>There must be a title to be considered VALID html</title>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?php
getCats();
?>
</ul>
</body>
</html>
i add this define() function in my config.php file.
$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_',$options['0']['sign']);
Now, i required config.php file into my index.php page like this :
require $abspath .'/config.php';
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?PHP echo _SIGN_; ?>
</body>
</html>
now in output i have this result:
?????????????????????
worked result :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_',$options['0']['sign']);
<?PHP echo _SIGN_; ?>
</body>
</html>
output result :
تواصل معنا
how do fix this for show unicode UTF-8 when i required config.php ?!
Fist where did you set the variable $abspath ?
Then you have to put <?php and ?> tags around your require statemet and in your config file around your php!
Now the example below works fine for me and is pretty much the same as yours!
config.php:
<?php
$options['0']['sign'] = "تواصل معنا"; //$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_', $options['0']['sign']);
?>
index.php:
<?php require 'config.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?PHP echo _SIGN_; ?>
</body>
</html>
And the Output is:
تواصل معنا
Did you try after establishing connection to the database execute:
set_charset ( "utf8")
I use MySQLi and make it this way:
//-> conncecting to db
$mysqli = new mysqli ( $hostname, $username, $password, $db );
if ( $mysqli -> connect_errno ) {
echo 'Failed to connect to MySQL: ' . $mysqli -> connect_error;
}
// change character set to utf8
if ( ! $mysqli -> set_charset ( "utf8") ) {
printf ( "Error loading character set utf8: %s\n", $mysqli->error );
}