PHP mysqli_select_db参数错误求助及代码问题排查
Fixing Your PHP Database & Session Issues
Hey there, let's tackle your problems step by step—since you're short on time and not a PHP pro, I'll keep this clear and actionable.
First, let's address the warning you're seeing:
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp\www\trynew\down_line.php on line 50
This is a simple parameter order mix-up. The mysqli_select_db() function expects the database connection object first, then the database name—you had them reversed.
Beyond that, there are a few other critical issues in your code that are causing functionality problems and security risks. Let's fix them one by one:
Key Fixes & Explanations
- Fix
mysqli_select_db()order: Swap the arguments tomysqli_select_db($db, "testing"); - Add connection to
mysqli_query:mysqli_query()requires the database connection as the first parameter—usemysqli_query($db, $query); - Fix Session assignment syntax: Your current
$row.$_SESSION['fullname'] = $Fname;is backwards and invalid. You need to pull values from the database row and assign them to the Session, like$_SESSION['fullname'] = $row['fullname'];(adjust the column name to match your actual database field) - Fix error handling for queries: Use
mysqli_error($db)instead ofmysqli_connect_error()when querying fails—connect_erroris only for database connection issues - Fix SQL Injection Risk: Never directly insert user input like
$unameinto a query. Usemysqli_real_escape_string()(quick fix) or prepared statements (more secure long-term)
Corrected Full Code
<?php // Start the session first (required for Sessions to work across pages) session_start(); $db = mysqli_connect("localhost", "joy", "hvkgigkgijhkhi"); if(!$db){ echo "No DB Connection"; } else { // Fixed: Correct mysqli_select_db parameter order mysqli_select_db($db, "testing"); // Fixed: Escape user input to block SQL injection $safe_uname = mysqli_real_escape_string($db, $uname); $query = "SELECT * FROM member WHERE uname= '$safe_uname'"; // Fixed: Pass database connection to mysqli_query $results = mysqli_query($db, $query); if(!$results){ // Fixed: Use query-specific error message die('Could not run query: ' . mysqli_error($db)); } else { // Fetch the matching user row (no need for a loop if uname is unique) $row = mysqli_fetch_array($results, MYSQLI_ASSOC); if($row) { // Only assign Sessions if a user was found // Fixed: Properly assign database values to Session variables $_SESSION['uname'] = $row['uname']; $_SESSION['fullname'] = $row['Fname']; // Adjust column name if your DB uses something else $_SESSION['my_ref_id'] = $row['My_ref']; $_SESSION['email'] = $row['email']; // $_SESSION['success'] = "You are now logged in"; // header('location: index.php'); } } } ?> <!-- logged in user information --> <div class="menue"> <?php if (isset($_SESSION['uname'])) : ?> <p>Refferal Pin: <strong><?php echo $_SESSION['my_ref_id']; ?></strong><br></p> <p>Full Name: <strong><?php echo $_SESSION['fullname']; ?></strong><br></p> <p>User Name: <strong><?php echo $_SESSION['uname']; ?></strong><br></p> <p>Email Address: <strong><?php echo $_SESSION['email']; ?></strong><br></p> <p> <a href="index.php?logout='1'" style="color: red;">logout</a> </p> <?php endif ?> </div>
Quick Additional Notes
- Ensure
session_start();is at the top of every page that uses Sessions—without it, your Session values won't persist - Double-check that column names in
$row['column_name']match exactly what's in yourmembertable (e.g., if your full name column isfull_nameinstead ofFname, adjust that) - For long-term security, consider switching to prepared statements (more robust against injection):
// Quick prepared statement example for your query $stmt = mysqli_prepare($db, "SELECT * FROM member WHERE uname = ?"); mysqli_stmt_bind_param($stmt, "s", $uname); mysqli_stmt_execute($stmt); $results = mysqli_stmt_get_result($stmt); $row = mysqli_fetch_array($results, MYSQLI_ASSOC);
内容的提问来源于stack exchange,提问作者Anthony Holland
相关产品推荐
相关产品推荐

